package com.scenic.rd_android.testsensor;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.nova.scenic.projectlibs.util.debug.MyLog;
import com.scenic.rd_android.R;

public class TestSensorActivity extends AppCompatActivity {

    private SensorManager sensormanager;
    MyLog mylog = new MyLog("TestSensorActivity");
    private Sensor sensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_sensor);

        sensormanager = (SensorManager) getSystemService(SENSOR_SERVICE);

        sensor = sensormanager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mylog.info("sen is " + sensor);
//        List<Sensor> list = sensormanager.getSensorList(Sensor.TYPE_ACCELEROMETER);
//        for (Sensor sensor : list) {
//            mylog.info("sensor is " + sensor);
//        }

        sensormanager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        sensormanager.unregisterListener(listener);
    }

    private SensorEventListener listener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {

            for (int i = 0; i <  event.values.length ; i++) {
                float f = event.values[i];
                if(Math.abs(f) > 5){
                    mylog.info("sensorEvent index " + i + "\t" + f);
                }
            }

        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            mylog.info("onAccuracyChanged " + sensor + " accuracy " + accuracy);
        }
    };
}
